//
//  CertificateMailCell.m
//  VideoExam
//
//  Created by apple on 2020/4/17.
//

#import "CertificateMailCell.h"
#import "CityPickerVeiw.h"
@interface CertificateMailCell ()
//@property(nonatomic,strong)UILabel * showTitleLabel;
@property(nonatomic,strong)UITextField * textFieled;
@property(nonatomic,strong)UIImageView * arrow_imageView;
@property(nonatomic,strong)UILabel * errorLabel;
@property(nonatomic,strong)UIColor * error_color;
@property(nonatomic,strong)UIView * textBaseView;
@end
@implementation CertificateMailCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self uiSet];
    }
    return self;
}
- (void)uiSet {
    self.error_color = color(@"#FF4C40");
    
    self.textBaseView = [[UIView alloc] initWithFrame:CGRectZero];
    self.textBaseView.layer.cornerRadius = 6;
    self.textBaseView.layer.masksToBounds = YES;
    self.textBaseView.backgroundColor = color(@"#F5F5F5");
    [self.contentView addSubview:self.textBaseView];
     [self.textBaseView mas_makeConstraints:^(MASConstraintMaker *make) {
           make.top.mas_equalTo(5);
           make.left.mas_equalTo(20);
           make.right.mas_equalTo(self.contentView.mas_right).offset(-20);
           make.height.mas_equalTo(50);
    }];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClicked:)];
    [self.textBaseView addGestureRecognizer:tap];
   
   
    self.arrow_imageView = [UIFactory create_ImageView_ImageName:@"向下箭头"];
    self.arrow_imageView.highlightedImage = UIImageMake(@"向上箭头");
    [self.textBaseView addSubview:self.arrow_imageView];
    [self.arrow_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.textBaseView);
        make.right.mas_equalTo(self.textBaseView.mas_right).offset(-16);
        make.width.mas_equalTo(24);
        make.height.mas_equalTo(24);
    }];
    
   self.textFieled = [[UITextField alloc] initWithFrame:CGRectZero];
//   self.textFieled.delegate = self;
    self.textFieled.font = [UIFont systemFontOfSize:16];
   self.textFieled.backgroundColor = color(@"#F5F5F5");
    [self.textFieled addTarget:self action:@selector(textField_text:) forControlEvents:UIControlEventEditingChanged];

   [self.textBaseView addSubview:self.textFieled];
   [self.textFieled mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.mas_equalTo(0);
       make.left.mas_equalTo(12);
       make.right.mas_equalTo(self.arrow_imageView.mas_left).offset(-12);
       make.height.mas_equalTo(self.textBaseView);
   }];
    
    self.errorLabel = [UIFactory create_LabelContent:@"错误信息" textColor:self.error_color fontSize:13];
    [self.contentView addSubview:self.errorLabel];
    [self.errorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.textBaseView.mas_bottom).offset(10);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-20);
        make.height.mas_equalTo(20);
    }];
        
    
}
- (void)setModel:(ExamineeInfoCellModel *)model {
    _model = model;
    self.arrow_imageView.highlighted = NO;
    self.textFieled.placeholder = _model.placeholder;
    self.textFieled.text = _model.content;
    self.errorLabel.text = _model.errorInfo;
    self.errorLabel.hidden = !_model.showError;
    if (_model.selected) {
        self.arrow_imageView.hidden = NO;
        self.textFieled.userInteractionEnabled = NO;
    } else {
        self.arrow_imageView.hidden = YES;
        self.textFieled.userInteractionEnabled = YES;
    }
}
- (void)textField_text:(UITextField *)textField {
    NSString * text = textField.text;
       GDLog(@"string = %@",text);
   if (self.model.row == 1) { //输入手机号
           self.model.content= [text removeSpaceAndNewline:text];
           BOOL error = [NSString verifyPhoneNumber:self.model.content];//验证手机号
           self.model.showError = !error;
           [self show_error_type:!error];
           
       } else if (self.model.row == 0) { //姓名
           self.model.content = text;
           if (self.model.content.length<=1  || self.model.content.length>30) {
                self.model.showError = YES;
               [self show_error_type:YES];
           } else {
               self.model.showError = NO;
               [self show_error_type:NO];
           }
       } else if (self.model.row == 3) { //详细的地址
           self.model.content = text;
           if ([self.model.content isEqualToString:@""]) {//不可为空
               self.model.showError = YES;
              [self show_error_type:YES];
              } else {
                  self.model.showError = NO;
                  [self show_error_type:NO];
              }
       }
       [self data_detection];
}
#pragma mark 输入框代理
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//    if (self.model.row == 1) { //输入手机号
//        if (![string isEqualToString:@""]) {
//            self.model.content =[NSString stringWithFormat:@"%@%@",textField.text,string];
//            if (textField.text.length == 3 || textField.text.length == 8 ) {
//                textField.text= [textField.text stringByAppendingFormat:@" "];
//            }
//        } else {
//            if (self.model.content.length>=1) {
//                self.model.content = [self.model.content substringToIndex:self.model.content.length - range.length];
//            }
//        }
//        self.model.content= [self.model.content removeSpaceAndNewline:self.model.content];
//        BOOL error = [NSString verifyPhoneNumber:self.model.content];//验证手机号
//        self.model.showError = !error;
//        [self show_error_type:!error];
//
//    }else if (self.model.row == 0) { //姓名
//        if (![string isEqualToString:@""]) {
//             self.model.content =[NSString stringWithFormat:@"%@%@",textField.text,string];
//        } else {
//            if (self.model.content.length>=1) {
//                self.model.content = [self.model.content substringToIndex:self.model.content.length - range.length];
//            }
//        }
//        if (self.model.content.length<=1  || self.model.content.length>11) {
//             self.model.showError = YES;
//            [self show_error_type:YES];
//        } else {
//            self.model.showError = NO;
//            [self show_error_type:NO];
//        }
//    } else if (self.model.row == 3) {//填写详细地址
//        if (![string isEqualToString:@""]) {
//             self.model.content =[NSString stringWithFormat:@"%@%@",textField.text,string];
//        } else {
//            if (self.model.content.length>=1) {
//                self.model.content = [self.model.content substringToIndex:self.model.content.length - range.length];
//            }
//        }
//        if ([self.model.content isEqualToString:@""]) {//不可为空
//             self.model.showError = YES;
//            [self show_error_type:YES];
//        } else {
//            self.model.showError = NO;
//            [self show_error_type:NO];
//        }
//    }
//    [self data_detection];
//    return YES;
//}

- (void)tapClicked:(UIGestureRecognizer *)tap {
    if (self.model.row==2) {
        UIViewController * vc = [AppTools zs_getCurrentViewController];
        [vc.view endEditing:YES];
         self.arrow_imageView.highlighted = YES;
        [CityPickerVeiw showCityName:self.model.content clickedBlock:^(NSString *vale) {
             self.model.content=vale;
            [self.tableView reloadData];
        }];
    }
    
}


- (void)data_detection {
    if (self.data_detection_block) {
        self.data_detection_block();
    }
}


#pragma mark 显示错误样式
- (void)show_error_type:(BOOL)error {
    if (error) {
        self.model.cellHeight = 5+50+30+5;
        self.textBaseView.layer.borderColor = [self.error_color CGColor];
        self.textBaseView.layer.borderWidth = 1;
        self.textFieled.textColor = self.error_color;
        self.errorLabel.hidden = NO;
    } else {
        self.model.cellHeight = 60;
        self.textFieled.textColor = [UIColor blackColor];
        self.textBaseView.layer.borderWidth = 0;
        self.errorLabel.hidden = YES;
    }
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

@end


#pragma mark CertificateMailShowCell

@interface CertificateMailShowCell ()
@property(nonatomic,strong)UIView  * baseView;
@property(nonatomic,strong)UILabel * titleLabel;
@property(nonatomic,strong)UILabel * contentLabel;
@property(nonatomic,strong)UIView * line;
@end

@implementation CertificateMailShowCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self uiSet];
    }
    return self;
}
- (void)uiSet {
    self.baseView = [[UIView alloc] initWithFrame:CGRectMake(20, 5, SCREEN_WIDTH-40, 50)];
    self.baseView.backgroundColor=color(@"#F5F5F5");
    self.baseView.layer.cornerRadius=6;
    self.baseView.layer.masksToBounds=YES;
    [self.contentView addSubview:self.baseView];
    
   
    
    
    self.titleLabel = [UIFactory create_LabelContent:@"" textColor:nil fontSize:16];
    self.titleLabel.frame=CGRectMake(15, 0, 80, self.baseView.height);
    [self.baseView addSubview:self.titleLabel];
    
    self.contentLabel = [UIFactory create_LabelContent:@"" textColor:nil fontSize:16];
    self.contentLabel.frame=CGRectMake(self.titleLabel.right+15, 0, self.baseView.width-30-self.titleLabel.right, self.baseView.height);
    self.contentLabel.numberOfLines=0;
    [self.baseView addSubview:self.contentLabel];
   
   
}
- (void)setContent:(NSString *)content {
    _content=content;
    self.contentLabel.text=_content;
    CGSize size= [self.contentLabel sizeThatFits:CGSizeMake(self.contentLabel.width, 50)];
    if (size.height<=50) {
        self.contentLabel.height=50;
    } else {
        self.contentLabel.top=5;
        self.contentLabel.height=size.height+5;
        if (self.indexPath.row==3) {
            self.baseView.height=self.contentLabel.bottom+5;
            self.line.top=self.baseView.bottom-0.5;
            if (self.cellHeightBlock) {
                self.cellHeightBlock(self.baseView.height);
            }
        }
    }
   
}

- (void)setIndexPath:(NSIndexPath *)indexPath {
    _indexPath=indexPath;
     if (_indexPath.row==0) {
        self.titleLabel.text=@"收件人";
    } else if (_indexPath.row==1) {
        self.titleLabel.text=@"电话";
    } else if (_indexPath.row==2) {
        self.titleLabel.text=@"城市";
    } else if (_indexPath.row==3) {
        self.titleLabel.text=@"";
    }
    if (_indexPath.row==3) {
        self.contentLabel.width=self.baseView.width-30;
    }
    CGSize size= [self.titleLabel sizeThatFits:CGSizeMake(CGFLOAT_MAX, 50)];
    self.titleLabel.width=size.width;
    if (_indexPath.row<3) {
         self.titleLabel.width=size.width+20;
    } else  {
         self.titleLabel.width=0;
    }
    self.contentLabel.left = self.titleLabel.right;
  
}
@end
